package com.guigu.qingcheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guigu.qingcheng.mapper.TbPreferentialMapper;
import com.guigu.qingcheng.pojo.TbPreferential;
import com.guigu.qingcheng.service.TbPreferentialService;
import com.guigu.qingcheng.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
*
* @author Jiang
* @date 2020/07/31
*/
@Service
public class TbPreferentialServiceImpl implements TbPreferentialService {
    @Autowired(required = false)
    private TbPreferentialMapper tbPreferentialMapper;


    @Override
    public List<TbPreferential> queryAll() {
        List<TbPreferential> tbPreferential = tbPreferentialMapper.selectAll();
            return tbPreferential;
    }

    @Override
    public PageUtil<TbPreferential> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbPreferential> pages = (Page<TbPreferential>) tbPreferentialMapper.selectAll();
        PageUtil<TbPreferential> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbPreferential> pageQueryByAll(Integer pageNum, Integer pageSize, TbPreferential tbPreferential) {

        Example example=new Example(TbPreferential.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO 待实现 重新实现判断参数

        //if(!StringUtils.isEmpty(brand.getId())){
        //    criteria.andEqualTo("id",brand.getId());
        //}
        //if(!StringUtils.isEmpty(brand.getName())){
        //   criteria.andLike("name","%"+brand.getName()+"%");
        //}
        PageHelper.startPage(pageNum,pageSize);
        Page<TbPreferential> pages = (Page<TbPreferential>) tbPreferentialMapper.selectByExample(example);
        PageUtil<TbPreferential> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    public boolean saveTbPreferential(TbPreferential tbPreferential) {
        int insert = tbPreferentialMapper.insert(tbPreferential);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbPreferential(TbPreferential tbPreferential) {
        int i = tbPreferentialMapper.updateByPrimaryKeySelective(tbPreferential);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = tbPreferentialMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbPreferential findById(Integer id) {
        TbPreferential tbPreferential = tbPreferentialMapper.selectByPrimaryKey(id);
        return tbPreferential;
    }

    @Override
    public int findPreMoneyByCategoryId(Integer categoryId, int money) {
        // 指定查询条件  状态  分类  时间  消费额降序排序
        Example example=new Example(TbPreferential.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("state","1"); //状态
        criteria.andEqualTo("categoryId",categoryId); // 分类
        criteria.andLessThanOrEqualTo("buyMoney",money); // 消费金额
        criteria.andGreaterThanOrEqualTo("endTime",new Date()); //活动截止日期大于当前时间
        criteria.andLessThanOrEqualTo("startTime",new Date()); //活动开始日期小于当前时间
        example.setOrderByClause("buy_money desc"); //按购买金额降序排序

        List<TbPreferential> tbPreferentials = tbPreferentialMapper.selectByExample(example);

        if(tbPreferentials.size()>=1){// 有优惠
            TbPreferential tbPreferential = tbPreferentials.get(0);
            if("1".equals(tbPreferential.getType())){
                // 不翻倍
               return tbPreferential.getPreMoney();
            }else {
                // 翻倍
                // 计算倍数
               int bei= money/tbPreferential.getBuyMoney();
               return tbPreferential.getPreMoney()*bei;
            }
        }else {
            // 没有优惠
            return 0;
        }
    }
}
